#include "errdiff.h"

#include <assert.h>

int main() {
    byte gray[IMG_H][IMG_W];

    int has_errors = 0;

    // read files
    FILE *input_fp = fopen("input.data", "r");
    assert(input_fp != NULL && "Couldn't open input data file");

    for (int i = 0; i < IMG_H; i++) {
        for (int j = 0; j < IMG_W; j++) {
            int pix;
            int res = fscanf(input_fp, "%d", &pix);
            assert(res == 1 && "read input data file failed");
            gray[i][j] = pix;
        }
    }
    fclose(input_fp);

    // process image
    errdiff(gray);

    FILE *check_fp = fopen("check.data", "r");
    assert(input_fp != NULL && "Couldn't open check data file");

    for (int i = 0; i < IMG_H; i++) {
        for (int j = 0; j < IMG_W; j++) {
            int pix;
            int res = fscanf(check_fp, "%d", &pix);
            assert(res > 0 && "read check data file failed");
            if (gray[i][j] != pix) {
                printf("gray[%d][%d] = %d, expected_gray[%d][%d] = %d\n",
                       i, j,
                       gray[i][j],
                       i, j,
                       pix);
                has_errors++;
            }
        }
    }

    fclose(check_fp);

    if (has_errors) {
        fprintf(stderr, "Benchmark results are incorrect\n");
        return -1;
    }

    printf("Success.\n");
    return 0;
}
